(function($) {

	var methods={
		init : function( options,callback ){
			var settings = $.extend({
				id:'#lang-select',
				listPosition:'dropdown',
				event:'click'
			},options);

			var $this=jQuery(this);

			if(jQuery(settings.id)) {
				methods.clickControl(settings.event);

				if(settings.event=='mouseenter') {
					jQuery(settings.id).bind(settings.event,function(e) {
						methods.subMenu(jQuery(this).find('a:first'),settings.listPosition,callback,settings.event);
					}).bind('mouseleave',function(e) {
						methods.hideSubMenu();
						e.stopPropagation();
					});
				} else {

					jQuery(settings.id).find('a:first').bind(settings.event,function(e) {
						methods.subMenu(jQuery(this),settings.listPosition,callback,settings.event);
						e.preventDefault();
					}).bind('mouseleave',function() {
						methods.clickControl(settings.event);
					});
				}

				jQuery(settings.id+' .list_wrap a').click(function(e) {
					methods.chooseList(jQuery(this),jQuery(this).parents('.link_select').attr('id'));
					jQuery(this).parents('.list_wrap').find('li').removeClass('on').each(function() {
					});
				}).mouseover(function() {
				}).mouseout(function() {
				});

			}

		},
		// 리스트 내용 중 선택된 내용으로 변경되도록
		setHtmlContent : function(element, html) {
			element.html(html);
		},
		// initialize document.click and dropdown menu 
		clickControl : function(event) {
			jQuery(document).bind('click',function() {
				methods.hideSubMenu();
			});
		},
		// initialize dropdown
		hideSubMenu : function() {
			jQuery('.list_wrap').each(function() {
				//jQuery(this).eq(0).hide('fast');
				jQuery(this)
					.eq(0).css('display','none')
					.end()
					.parents('.link_select').removeClass('link_select_on').find('a:first').attr('class','se');
			});
		},
		// active dropdown menu
		subMenu : function(obj,position,callback,event) {
			if(!obj.attr('class').match('se_on')){
				methods.hideSubMenu();
				$(document).unbind('click');

				obj.parents('.link_select').find('.list_wrap').eq(0).css('display','block').css({height:obj.parents('.link_select').find('.list_wrap ul').height()+'px'})

				if(position=='dropup') {
					obj.parents('.link_select').find('.list_wrap').css(
					{
						'position':'absolute',
						'top':(obj.parents('.link_select').find('.list_wrap').height()+obj.parents('.link_select').find('a:first').height()-Math.ceil(obj.parents('.link_select').find('a:first').height()-25))*-1+'px',
						'left':0,
						'width':obj.parents('.link_select').width()-2
					});
				} else {
					obj.parents('.link_select').find('.list_wrap').css(
					{
						'top':obj.parents('.link_select').find('a:first').height()-1+'px',
						'width':obj.parents('.link_select').width()-2
					});
				}

				obj.first().attr('class','se_on').end().parents('.link_select').css('z-index','2').addClass('link_select_on');
			} else {
				methods.hideSubMenu();
			}
		},
		// select dropdown/up
		chooseList : function(obj,type) {

			var swap=null,swapRel=null;
			var selectAlink = obj.parents('.link_select').find('a').eq(0);
			var selectText = obj.parents('.link_select').find('a > strong').eq(0);
			
			swap = selectAlink.html();
			swapRel = selectAlink.rel;


			//this.setHtmlContent(selectText,obj.html());
			selectAlink.attr('rel',obj.attr('rel'));

			if(typeof callback == 'function') {
				callback.call(this);
			}
			
			/* matching IP & save cookie
			this.checkIP();
			this.saveCookie(); */
		}/*,
		checkIP : function(ip){
			//alert('test');
		},
		saveCookie : function saveCookie(){
		}*/
		
	};

	$.fn.selectlink = function( method ){
		// Method calling logic
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.selectlink' );
		}    
	};
})(jQuery);

/*
	$('#wrapper').selectlink({
		id:'#lang-select',
		listPosition:'dropup'
	},function(){
		//callback function executed at select list click
		//ex) checkIP(), saveCookie()
	});
*/